package org.yu.register.recipe;

import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.NonNullList;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.util.RecipeMatcher;
import net.minecraftforge.items.wrapper.RecipeWrapper;
import org.yu.api.register.LazyRecipe;
import org.yu.api.register.LazyRecipeSerializer;
import org.yu.api.register.RecipeHolder;
import org.yu.api.util.LazyUtil;
import org.yu.register.RecipeRegister;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public final class ImpactRecipe extends LazyRecipe {
    @Expose
    public final NonNullList<Ingredient> input;
    @Expose
    public final String result;

    public ImpactRecipe(NonNullList<Ingredient> input, String result) {
        this.input = input;
        this.result = result;
    }

    @Nonnull
    @Override
    protected RecipeHolder<?> getRecipeHolder() {
        return RecipeRegister.impactRecipe;
    }

    public String getResult() {
        return result;
    }

    public boolean matches(RecipeWrapper pInv, Level pLevel) {
            if(pInv.getContainerSize()!=input.size())return false;
            for(int i=0;i<input.size();i++){
                var inputItem=input.get(i);
                var invItem=pInv.getItem(i);
                if(inputItem.isEmpty()&&invItem.isEmpty())continue;
                if(!inputItem.test(invItem))
                    return false;
            }
        return false;
    }
    @Override
    public ItemStack assemble(RecipeWrapper pContainer) {
        return ItemStack.EMPTY;
    }

    @Override
    public ItemStack getResultItem() {
        return ItemStack.EMPTY;
    }
    public static final class Serializer extends LazyRecipeSerializer<ImpactRecipe>{

        @Override
        public ImpactRecipe fromJson(ResourceLocation pRecipeId, JsonObject pSerializedRecipe) {
            var result= GSON.fromJson(pSerializedRecipe,ImpactRecipe.class);
            result.setId(pRecipeId);
            return  result;
        }

        @Nonnull
        @Override
        public ImpactRecipe fromNetwork(ResourceLocation pRecipeId, FriendlyByteBuf pBuffer) {
            var result= GSON.fromJson(pBuffer.readUtf(),ImpactRecipe.class);
            result.setId(pRecipeId);
            return  result;
        }

        @Override
        public void toNetwork(FriendlyByteBuf pBuffer, ImpactRecipe pRecipe) {
            pBuffer.writeUtf(GSON.toJson(pRecipe));
        }
    }
}
